<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/13
 * Time: 上午11:50
 */

namespace app\src\profit\logic;


use app\src\base\logic\BaseLogic;
use app\src\profit\model\Profit;


class ProfitLogic extends BaseLogic{
    const SCORE_MEMBER = 0;
    const SCORE_PLATFORM = 1;
    const SCORE_SELLER = 2;
    public function _init()
    {
        $this->setModel(new Profit());
    }

    /*
         *查询用户的佣金
         */
    public function queryAccounts($type,$uid){
        $map = [];
        if($type == ProfitLogic::SCORE_MEMBER){
            //$map['_string'] = 'p0='.$uid.' OR p1='.$uid.' OR p1_5='.$uid.' OR p3='.$uid;
            $map['p0|p1|p1_5|p3'] = $uid;
            $field = 'uid,p0,s0,p1,s1,p1_5,s1_5,p3,s3,type,order_id,create_time';
        }elseif($type == ProfitLogic::SCORE_PLATFORM){
            $map = ['p4'=>$uid];
            $field = 'uid,p4,s4,type,order_id,create_time';
        }elseif($type == ProfitLogic::SCORE_SELLER){
            $map = ['p3'=>$uid];
            $field = 'uid,p4,s4,type,order_id,create_time';
        }

        $result = $this->queryNoPaging($map,'',$field);
        if($type == ProfitLogic::SCORE_MEMBER){
            if(!empty($result['info'])){

                $tem = [];
                foreach($result['info'] as $k=>$v){
                    $tem[] = $this->un_set($v,$uid);
                }
            }

            if($result['status']){
                return $this->apiReturnSuc($tem);
            }else{
                return $this->apiReturnErr($result['info']);
            }
        }


        if($result['status']){
            return $this->apiReturnSuc($result['info']);
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }


    public function un_set($v,$uid){
        dump($v);exit;
        $data =$v;
        $tem =[];
        foreach(['0','1','1_5','3'] as $vv){
            $p='p'.$vv;$s='s'.$vv;
            upset($data[$p]);upset($data[$s]);
        }

        if($v['p0']==$uid){
            $data_1 = $data;
            $data_1['note'] ='自己消费获利';
            $data_1['p'] = $v['p0'];
            $data_1['s'] = $v['s0'];
            $tem[]=$data_1;

        }
        if($v['p1']==$uid){
            $data_2 = $data;
            $data_2['note'] ='下级消费获利';
            $data_2['p'] = $v['p1'];
            $data_2['s'] = $v['s1'];
            $tem[]=$data_2;

        }
        if($v['p1_5']==$uid){
            $data_3 = $data;
            $data_3['note'] ='下级的下级消费获利';
            $data_3['p'] = $v['p1_5'];
            $data_3['s'] = $v['s1_5'];
            $tem[]=$data_3;
        }
        if($v['p3']==$uid){
            $data_4 = $data;
            $data_4['note'] ='作为店长获取';
            $data_4['p'] = $v['p3'];
            $data_4['s'] = $v['s3'];
            $tem[]=$data_4;
        }



        return($tem);
    }

}